<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/29 18:03
 * Remark:
 */

namespace App\Func;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Models\File\FileModel;
use App\Models\Staff\StaffModel;
use App\Service\Approval\PublicDingTalkApprovalService;
use Illuminate\Http\Exceptions\HttpResponseException;

class ApprovalFunc
{
    /**
     * 通过用户数组获取钉钉数组
     * 如果存在未绑定钉钉的用户 则返回 { ok : false ; data 未绑定用户的id}
     * @param $userIds
     * @return array
     */
    public static function getDingTalkUserIdByUserIds($userIds): array
    {
        $staffModel = new StaffModel();
        $dingTalkUserIds = $staffModel->getDingTalkUserIdByUserIds($userIds,["staff_id","staff_dingding_account"]);
        $dingTalkFormat = array_column($dingTalkUserIds->toArray(),'staff_dingding_account','staff_id');
        foreach($dingTalkFormat as $key => $dingTalkUserId){
            if(!$dingTalkUserId){
               return [
                   'ok' => false,
                   'data' => $key
               ];
            }
        }
        return [
            'ok' => true,
            'data' =>$dingTalkFormat
        ];
    }

    /**
     * 前端数据审批发起
     * 来源类型
     * 来源Id
     * 前端数据
     * @param $type
     * @param $sourceId
     * @param $webData
     */
    public static function getDingTalkApprovalByWeb($type,$sourceId,$webData,$canReturnResult = true)
    {
        $dd_dept_id = request()->all()['dd_dept_id'] ?? -1;
        $sqlComponent = $webData;
        $pb = new PublicDingTalkApprovalService();
        // 处理文件
        $files = [];
        foreach($webData as $key=>$value){
            if(isset($value['isFile'])){
                $files[]= $value;
                unset($webData[$key]);
            }
        }
        if($files){
            $fileModel = new FileModel();
            foreach($files as $value){
                $fileIds = array_column($value['value'],'file_id');
                $getFileList = $fileModel->getFilesByIdSelect($fileIds,["dingTalkSpaceInfo"])->toArray();
                $getFileFmtList = array_column($getFileList,"dingTalkSpaceInfo");
                $fileData = [];
                foreach($getFileFmtList as $vv){
                    $fileData[] = json_decode($vv,true);
                }
                $webData[] = [
                    'name' => $value['name'],
                    'value' => $fileData,
                ];
            }

        }
        try {
            $res = $pb->sendApproval(
                $type,
                $sourceId,
                $webData,
                $dd_dept_id,
                [
                    'sqlComponent' => $sqlComponent,
                    'file_ids' => "",
                    'remark' => ""
                ]
            );
            if($canReturnResult){
                return ResponseFunc::returnJson(
                    'ok',
                    [
                        "ding_talk_approval_id" => $res
                    ],
                    '发起审批实例成功',
                    ResponseStatusConsts::OK_200_RESPONSE_STATUS,
                );
            }else{
                return $res;
            }
        }catch (\Exception $exception){
            $exceptionMsg = @$exception->getResponse()->getContent() ? json_decode($exception->getResponse()->getContent(),true)['msg'] : "";
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                $exceptionMsg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }

    }





}